package com.itany.shop.controller;

import com.itany.shop.exception.LoginDisabledException;
import com.itany.shop.exception.RequestParameterErrorException;
import com.itany.shop.factory.ObjectFactory;
import com.itany.shop.service.CartService;
import com.itany.shop.vo.Cart;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;

/**
 * @Author:石小俊
 * @Date:2022/12/16 11:41
 * @Version:1.0-SNAPSHOT
 * @Description:
 */
@WebServlet("*.cart")
public class CartServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        request.setCharacterEncoding("utf-8");
//        response.setContentType("text/html;charset=utf-8");
        String path = request.getServletPath();
        if("/add.cart".equals(path)){
            addCart(request,response);
        }
        if("/removeById.cart".equals(path)){
            removeById(request,response);
        }
        if("/removeByIds.cart".equals(path)){
            removeByIds(request,response);
        }
        if("/modify.cart".equals(path)){
            modifyCart(request,response);
        }
        if("/clean.cart".equals(path)){
            clean(request,response);
        }
    }

    public void clean(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 清空购物车并不是销毁,而是将购物车中的数据清除
        // 可以使用一个新的初始购物车替换当前的
        request.getSession().setAttribute("cart",new Cart());
        response.sendRedirect(request.getContextPath()+"/jsp/cart.jsp");
    }

    public void removeByIds(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        CartService cartService = (CartService) ObjectFactory.getObject("cartService");
        String[] ids = request.getParameterValues("id");
        Cart cart = (Cart) request.getSession().getAttribute("cart");
        System.out.println("ids:"+ Arrays.toString(ids));

        try {
            for(String id : ids){
                cartService.removeCart(cart,id);
            }
            response.sendRedirect(request.getContextPath()+"/jsp/cart.jsp");
        } catch (LoginDisabledException e) {
            request.setAttribute("loginMsg",e.getMessage());
            request.getRequestDispatcher("/jsp/login.jsp").forward(request,response);
        } catch (RequestParameterErrorException e) {
            request.setAttribute("cartMsg",e.getMessage());
            request.getRequestDispatcher("/jsp/cart.jsp").forward(request,response);
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("cartMsg","服务器内部异常");
            request.getRequestDispatcher("/jsp/cart.jsp").forward(request,response);
        }

    }

    public void removeById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        CartService cartService = (CartService) ObjectFactory.getObject("cartService");
        Cart cart = (Cart) request.getSession().getAttribute("cart");
        String id = request.getParameter("id");

        try {
            cartService.removeCart(cart,id);
            response.sendRedirect(request.getContextPath()+"/jsp/cart.jsp");
        } catch (LoginDisabledException e) {
            request.setAttribute("loginMsg",e.getMessage());
            request.getRequestDispatcher("/jsp/login.jsp").forward(request,response);
        } catch (RequestParameterErrorException e) {
            request.setAttribute("cartMsg",e.getMessage());
            request.getRequestDispatcher("/jsp/cart.jsp").forward(request,response);
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("cartMsg","服务器内部异常");
            request.getRequestDispatcher("/jsp/cart.jsp").forward(request,response);
        }

    }

    public void modifyCart(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        CartService cartService = (CartService) ObjectFactory.getObject("cartService");
        String id = request.getParameter("id");
        String num = request.getParameter("num");
        Cart cart = (Cart) request.getSession().getAttribute("cart");

        try {
            cartService.modifyCart(cart,id,num);
            response.sendRedirect(request.getContextPath()+"/jsp/cart.jsp");
        } catch (LoginDisabledException e) {
            request.setAttribute("loginMsg",e.getMessage());
            request.getRequestDispatcher("/jsp/login.jsp").forward(request,response);
        } catch (RequestParameterErrorException e) {
            request.setAttribute("cartMsg",e.getMessage());
            request.getRequestDispatcher("/jsp/cart.jsp").forward(request,response);
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("cartMsg","服务器内部异常");
            request.getRequestDispatcher("/jsp/cart.jsp").forward(request,response);
        }
    }

    public void addCart(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        CartService cartService = (CartService) ObjectFactory.getObject("cartService");
        // 从session取出当前购物车
        Cart cart = (Cart) request.getSession().getAttribute("cart");
        // 获取请求参数
        String id = request.getParameter("id");

        try {
            cartService.addCart(cart,id);
            response.sendRedirect(request.getContextPath()+"/findAll.product");
        } catch (LoginDisabledException e) {
            request.setAttribute("loginMsg",e.getMessage());
            request.getRequestDispatcher("/jsp/login.jsp").forward(request,response);
        } catch (RequestParameterErrorException e) {
            request.setAttribute("productMsg",e.getMessage());
            request.getRequestDispatcher("/jsp/findAll.product").forward(request,response);
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("productMsg","服务器内部异常");
            request.getRequestDispatcher("/jsp/findAll.product").forward(request,response);
        }
    }
}
